using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Programonica.Igramonica.LandscapeClasses
{
    public class LandscapeNode
    {
        public enum NodeHeads
        {
            NorthWest,
            NorthEast,
            SouthWest,
            SouthEast,
        }

        protected LandscapeNode m_Parent;

        protected Dictionary<NodeHeads, LandscapeNode> m_Childs = new Dictionary<NodeHeads, LandscapeNode>();

        protected BoundingBox m_BoundingBox = new BoundingBox();

        protected Vector3 m_NodeCenter = Vector3.Zero;

        protected bool m_Drawn = false;

        public LandscapeNode NorthWest
        {
            get
            {
                return m_Childs[NodeHeads.NorthWest];
            }
        }

        public LandscapeNode NorthEast
        {
            get
            {
                return m_Childs[NodeHeads.NorthEast];
            }
        }

        public LandscapeNode SouthWest
        {
            get
            {
                return m_Childs[NodeHeads.SouthWest];
            }
        }

        public LandscapeNode SouthEast
        {
            get
            {
                return m_Childs[NodeHeads.SouthEast];
            }
        }

        public BoundingBox BoundingBox
        {
            get
            {
                return m_BoundingBox;
            }
        }


        public LandscapeNode()
        {
        }

        public virtual void Build(LandscapeNode northWest, LandscapeNode northEast, LandscapeNode southWest, LandscapeNode southEast)
        {
            northWest.m_Parent = this;
            northEast.m_Parent = this;
            southWest.m_Parent = this;
            southEast.m_Parent = this;

            m_Childs.Clear();

            m_Childs.Add(NodeHeads.NorthWest, northWest);
            m_Childs.Add(NodeHeads.NorthEast, northEast);
            m_Childs.Add(NodeHeads.SouthWest, southWest);
            m_Childs.Add(NodeHeads.SouthEast, southEast);

            foreach (LandscapeNode node in m_Childs.Values)
            {
                if (m_BoundingBox == new BoundingBox())
                {
                    m_BoundingBox = node.m_BoundingBox;
                }
                else
                {
                    m_BoundingBox = BoundingBox.CreateMerged(m_BoundingBox, node.m_BoundingBox);
                }
            }

            m_NodeCenter = Vector3.Divide(m_BoundingBox.Max + m_BoundingBox.Min, 2.0f);
        }

        public virtual void PrepareForDrawing()
        {
            m_Drawn = false;

            foreach (LandscapeNode node in m_Childs.Values)
            {
                node.PrepareForDrawing();
            }
        }

        public virtual LandscapeNode[] GetNodesToDraw(LOD lod)
        {
            if (NodeIsInLODFrustum(lod))
            {
                List<LandscapeNode> resultNodes = new List<LandscapeNode>();

                foreach (LandscapeNode childNode in m_Childs.Values)
                {
                    resultNodes.AddRange(childNode.GetNodesToDraw(lod));
                }

                m_Drawn = true;

                return resultNodes.ToArray();
            }

            return new LandscapeNode[] { };
        }

        protected bool NodeIsInLODFrustum(LOD lod)
        {
/*
            BoundingFrustum frustum = Camera.GlobalFrustum;
            if (lod == LOD.High)
            {
                frustum = Camera.LODHighFrustum;
            }
            else if (lod == LOD.Medium)
            {
                frustum = Camera.LODMediumFrustum;
            }
            else if (lod == LOD.Low)
            {
                frustum = Camera.LODLowFrustum;
            }
*/

            return false;
        }
    }
}
